/*
 * Copyright (C) 2013 The i18nCTS Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.i18nCTS.provider;

import java.util.ArrayList;

import android.util.Log;

class i18nCTSDatabase {
	
	final static String TAG = "I18nCTS_" + i18nCTSDatabase.class.getSimpleName();

	public class i18nCTSDataSet {
		
		class i18nCTSData {
		    String mResult = null;
		    String mExpected = null;
		    String mMisc = null;
		    
		    public i18nCTSData(String result, String misc) {
		    	// convert non-breaking space to a space
		    	mResult = result.replace('\u00A0', '\u0020');
		    	mExpected = "";
		    	mMisc = misc;
		    }
		    
		    public String getExpected() {
		    	return mExpected;
		    }
		    
		    public void setExpected(String expected) {
		    	mExpected = expected;
		    }

		    public String getResult() {
		    	return mResult;
		    }
		    
		    public String getMisc() {
		    	return mMisc;
		    }
		}
		
		private String mSubtitle = null;
		ArrayList<i18nCTSData> mListData = null;
	    
		public i18nCTSDataSet() {
			mListData = new ArrayList<i18nCTSData>();
		}
		
	    public void addSubTitle(String subtitle) {
	    	mSubtitle = subtitle;
	    }
	    public String getSubTitle() {
	    	return mSubtitle;
	    }
	    
	    public void addData(ArrayList<i18nCTSData> results) {
	    	mListData = results;
	    }
	    
	    public void addData(i18nCTSData data) {
	    	if (mListData != null)
	    		mListData.add(data);
	    	else
	    		Log.e(TAG, "addData(i18nCTSData): mListData is null");
	    }
	    
	    public void addData(String result, String misc) {
	    	if (mListData != null)
	    		mListData.add(new i18nCTSData(result, misc));
	    	else
	    		Log.e(TAG, "addData(String result, String misc): mListData is null");
	    }
	    
	    public ArrayList<i18nCTSData> getData() {
	    	return mListData;
	    }
	}

	private ArrayList<i18nCTSDataSet> mi18nCTSTable = null;
	private i18nCTSDataSet mCurDataSet = null;
	
	public i18nCTSDatabase () {
		mi18nCTSTable = new ArrayList<i18nCTSDataSet>();
		mCurDataSet = new i18nCTSDataSet();
	}
	
	public ArrayList<i18nCTSDataSet> getTable() {
		return mi18nCTSTable;
	}
	
	public i18nCTSDataSet getDataSet() {
		if (mCurDataSet == null) {
			Log.e(TAG, "getDataSet(): mCurDataSet is null");
			mCurDataSet = new i18nCTSDataSet();
		}
		return mCurDataSet;
	}

	public String getTitle() {
		if (mCurDataSet != null) {
			return mCurDataSet.mSubtitle;
		} else {
			Log.e(TAG, "getTitle(): mCurDataSet is null");
			return null;
		}
	}
	
	public void addTitle(String subtitle) {
		if (mCurDataSet == null)
			mCurDataSet = new i18nCTSDataSet();
		
		mCurDataSet.addSubTitle(subtitle);
	}
	
	public void addData(String result, String misc) {
		if (mCurDataSet == null)
			mCurDataSet = new i18nCTSDataSet();
		
		mCurDataSet.addData(result, misc);
	}
	
	public boolean commit() {
		// end of set
		if (mi18nCTSTable != null) {
			mi18nCTSTable.add(mCurDataSet);
			mCurDataSet = null;
			return true;
		} else {
			Log.e(TAG, "commit(): mi18nCTSTable is null");
			return false;			
		}
	}
}

